//
//  LXReturnCarRecordViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/22.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXLoanRecordViewController.h"
#import "LXStatisticsListTableViewCell.h"
#import "LXLoanApplyViewController.h"
#import "LXLoanModel.h"
@interface LXLoanRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger page;

@end

@implementation LXLoanRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    
    [self layoutCustomNav:self.view title:@"贷款车辆"];
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 70, KStatuesBarHeight + 5, 60, 40)];
   rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
   [rightButton setTitle:@"新增" forState:UIControlStateNormal];
    [rightButton setImage:Image(@"icon_add") forState:UIControlStateNormal];
   [rightButton setTitleColor:mainColor forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:rightButton];
    
    
    [self.view addSubview:self.tableView];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];

    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.page++;
        [self loadData];
    }];
    
 
    
}

- (void)loadData{

    NSDictionary *dic = @{
        @"rows":@(20),
        @"page":@(self.page),
        @"carId":self.carModel.ID
    };

    [MSNetworkManager getRevertUrl:GET_TbuycayloanPage params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if (isSuccess) {
            
            if (self.page==1) {
                [self.data removeAllObjects];
            }
            NSInteger totalPage = [resonseObj[@"total"] integerValue];
            
            NSArray *records = resonseObj[@"records"];
            
            [self.data addObjectsFromArray:[LXLoanModel  mj_objectArrayWithKeyValuesArray:records]];
            
            
            if (self.data.count>=totalPage) {
                self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            }
            else {
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
        }
        
            
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}



- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"cell";
    LXStatisticsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXStatisticsListTableViewCell class]) owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
    }
    LXLoanModel *model = self.data[indexPath.row];
    cell.loanModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    LXLoanModel *model = self.data[indexPath.row];

    
    NSDictionary *dic = @{
        @"id":model.ID,
    };

    [MSNetworkManager getRevertUrl:POST_AddTbuycayloan params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
    
        LXLoanModel *mm = [LXLoanModel  mj_objectWithKeyValues:resonseObj];
        LXLoanApplyViewController *vc = [[LXLoanApplyViewController alloc] init];
        vc.type = 2;
        mm.totalPeriods = model.totalPeriods;
        mm.paidPeriods = model.paidPeriods;
        vc.loanModel = mm;
        [self pushToNextController:vc];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];

  
}





- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, kScreenWidth, kScreenHeight - kViewTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 158;
        _tableView.backgroundColor = ColorWithF7F8FA;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_header.state = MJRefreshStateIdle;
        [self.tableView.mj_footer resetNoMoreData];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}


-(void)clickBtn{
    LXLoanApplyViewController *vc = [[LXLoanApplyViewController alloc] init];
    vc.type = 1;
    
    LXLoanModel *model = [LXLoanModel new];
    model.carNumber = self.carModel.carNum;
    model.carId = self.carModel.ID;
    model.carModel = [NSString stringWithFormat:@"%@ %@",self.carModel.carBrand,self.carModel.carMode];
    vc.loanModel = model;
    [self pushToNextController:vc];
    
}

@end
